package com.example.smartcitydemo.ui.smartbus.fragment

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.blankj.utilcode.util.ToastUtils
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.databinding.FragmentSmartBusSeccondBinding
import com.example.smartcitydemo.viewModel.smartBus.SmartBusViewModel
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.TimeFormat
import java.util.*

/**
 * @PACKAGE_NAME: com.example.smartcity_jscs.ui.smartbus.fragment
 * @NAME: Caleb.F
 * @DATE: 2021/8/25 18:54
 * @Email: 1919796283@qq.com
 */
class SmartBusSecondFragment :
    BaseFragment<FragmentSmartBusSeccondBinding>(R.layout.fragment_smart_bus_seccond) {
    private val busViewModel: SmartBusViewModel by activityViewModels()
    override fun addObservers(myViewBinding: FragmentSmartBusSeccondBinding) {
        busViewModel.timeString.observe(viewLifecycleOwner) {
            if (it.isNotBlank()) {
                val rawString = it.trim().split("日")
                val dateString = rawString[0]
                val timeString = rawString[1]
                myViewBinding.apply {
                    tvSubwaySecondDate.visibility = View.VISIBLE
                    tvSubwaySecondDate.text = "${dateString}日"
                    tvSubwaySecondTime.visibility = View.VISIBLE
                    tvSubwaySecondTime.text = timeString
                }
            }
        }

    }

    override fun initView(
        myViewBinding: FragmentSmartBusSeccondBinding,
        savedInstanceState: Bundle?
    ) {
        myViewBinding.apply {
            toolBarSmartSecond.tvContent.text = "日期选择"
            smartBusSecondBottom.btnSmartBusPrevious.setOnClickListener {
                this@SmartBusSecondFragment.findNavController().navigateUp()
            }
            smartBusSecondBottom.btnSmartBusNext.setOnClickListener {
                this@SmartBusSecondFragment.findNavController()
                    .navigate(R.id.action_navigation_smart_bus_second_to_navigation_smart_bus_third)
            }
            btnSubwayDate.setOnClickListener {
                // TODO: 2021/8/25 新款日历
                val datePicker = MaterialDatePicker.Builder.datePicker().setTitleText("请选择日期")
                    .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
                    .build()
                // TODO: 2021/8/26 先调用show再调用addListener
                datePicker.show(childFragmentManager, "efe")
                datePicker.addOnPositiveButtonClickListener {
                    val instance = Calendar.getInstance()
                    instance.timeInMillis = it
                    val dateString =
                        "${instance.get(Calendar.YEAR)}年${instance.get(Calendar.MONTH)}月${
                            instance.get(Calendar.DATE)
                        }日"
                    tvSubwaySecondDate.apply {
                        visibility = View.VISIBLE
                        text = dateString
                    }
                }
            }
            btnSubwayTime.setOnClickListener {
                val timePicker =
                    MaterialTimePicker.Builder().setHour(12).setMinute(34).setTitleText("选择时间")
                        .setInputMode(MaterialTimePicker.INPUT_MODE_KEYBOARD)
                        .setTimeFormat(TimeFormat.CLOCK_24H)
                        .build()
                timePicker.show(childFragmentManager, "dwd")
                timePicker.addOnPositiveButtonClickListener {
                    tvSubwaySecondTime.visibility = View.VISIBLE
                    val timeString = "${timePicker.hour}时${timePicker.minute}分"
                    tvSubwaySecondTime.text = timeString
                }
            }
            smartBusSecondBottom.btnSmartBusPrevious.setOnClickListener {
                this@SmartBusSecondFragment.findNavController()
                    .navigateUp()

            }
            smartBusSecondBottom.btnSmartBusNext.setOnClickListener {
                if (tvSubwaySecondTime.isVisible && tvSubwaySecondDate.isVisible) {
                    val string =
                        "${tvSubwaySecondDate.text.toString()}${tvSubwaySecondTime.text.toString()}"
                    ToastUtils.showLong(string)
                    busViewModel.putTimeString(string)
                    this@SmartBusSecondFragment.findNavController()
                        .navigate(R.id.action_navigation_smart_bus_second_to_navigation_smart_bus_third)

                } else {
                    ToastUtils.showLong("请选择日期与时间")
                }

            }
        }
    }

    override fun initData() {

    }
}